package com.dbsop.domain.transport.query;

import com.dbsop.common.enums.FlagConnect;
import com.dbsop.common.util.FieldUtils;
import com.dbsop.operation.query.transport.TcAnalyzeRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class AnalyzeQuery extends ConditionQuery {


    private String analyze = "";

    private List<String> values = new ArrayList<String>();


    public AnalyzeQuery(String columnName, String analyze) {
        super(columnName);
        this.analyze = analyze;
    }

    public AnalyzeQuery(String columnName, String analyze, List<String> values) {
        super(columnName);
        this.analyze = analyze;
        this.values = values;
    }

    public AnalyzeQuery(String columnName, FlagConnect flagConnect, String analyze, List<String> values) {
        super(columnName, flagConnect);
        this.analyze = analyze;
        this.values = values;
    }

    public AnalyzeQuery(String columnName, List<String> values) {
        super(columnName);
        this.values = values;
    }

    public AnalyzeQuery(String columnName, FlagConnect flagConnect, List<String> values) {
        super(columnName, flagConnect);
        this.values = values;
    }

    @Override
    public QueryBuilder extractBoolQuery() {
        TcAnalyzeRequest esAnalyzeRequest = new TcAnalyzeRequest(analyze, FieldUtils.converList(values));
        Set<String> texts = esAnalyzeRequest.analyzeRequest();
        if (texts == null || texts.size()==0)return new BoolQueryBuilder();
        return QueryBuilders.termsQuery(getColumnName(), FieldUtils.converList(texts));
    }


    public String getAnalyze() {
        return analyze;
    }

    public void setAnalyze(String analyze) {
        this.analyze = analyze;
    }

    public List<String> getValues() {
        return values;
    }



    public void add(String ... objs){
        values.addAll(Arrays.stream(objs).collect(Collectors.toList()));
    }
}
